<template>
  <div class="layout-container">
    <!-- 顶部区域：logo、系统名称、登录信息 -->
    <el-header class="top-header">
      <div class="header-content">
        <div class="header-left">
          <div class="logo">
            <el-icon size="32" color="#409eff">
              <Management />
            </el-icon>
          </div>
          <div class="system-title">
            <h1>{{ $t('app.title') }}</h1>
            <span class="system-subtitle">用户中心管理系统</span>
          </div>
        </div>
        <div class="header-right">
          <div class="user-info">
            <el-icon size="18" style="margin-right: 8px;">
              <User />
            </el-icon>
            <span>管理员</span>
          </div>
          <el-select 
            v-model="locale" 
            size="small" 
            style="width:120px;margin:0 12px" 
            @change="changeLocale"
          >
            <el-option label="中文" value="zh" />
            <el-option label="English" value="en" />
          </el-select>
          <el-button link @click="logout" class="logout-btn">
            <el-icon style="margin-right: 4px;">
              <SwitchButton />
            </el-icon>
            退出
          </el-button>
        </div>
      </div>
    </el-header>

    <!-- 底部区域：左右布局 -->
    <div class="bottom-container">
      <!-- 左侧菜单 -->
      <el-aside class="menu-aside">
        <el-menu 
          router 
          :default-active="$route.path"
          class="menu-container"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409eff"
        >
          <MenuTree :data="menus" />
        </el-menu>
      </el-aside>

      <!-- 右侧内容区域 -->
      <el-main class="content-main">
        <TabsView />
        <Breadcrumb />
        <div class="content-area">
          <router-view :key="contentKey" />
        </div>
      </el-main>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { ref, onMounted, onUnmounted } from 'vue'
import { Management, User, SwitchButton } from '@element-plus/icons-vue'
import TabsView from '../components/TabsView.vue'
import MenuTree from '../components/MenuTree.vue'
import Breadcrumb from '../components/Breadcrumb.vue'
import { useMenuStore } from '../stores/menu'
import request from '../utils/request'
import { useAuthStore } from '../stores/auth'

const router = useRouter()
const { locale: i18nLocale } = useI18n()
const locale = ref(localStorage.getItem('locale') || 'zh')
const menuStore = useMenuStore()
const menus = menuStore.$state.menus
const auth = useAuthStore()
const contentKey = ref(0)

function logout() {
  request.post('/api/auth/logout').finally(() => {
    auth.logout()
    router.push('/login')
  })
}

function changeLocale(val) {
  i18nLocale.value = val
  localStorage.setItem('locale', val)
}

// 处理页签刷新事件
function handleTabRefresh(event) {
  const { path } = event.detail
  const currentPath = router.currentRoute.value.path
  if (currentPath === path) {
    // 通过改变key值强制重新渲染router-view
    contentKey.value++
  }
}

onMounted(() => { 
  menuStore.load()
  // 监听页签刷新事件
  window.addEventListener('tab-refresh', handleTabRefresh)
})

onUnmounted(() => {
  // 清理事件监听器
  window.removeEventListener('tab-refresh', handleTabRefresh)
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 顶部区域样式 */
.top-header {
  height: 80px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-content {
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  margin-right: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
}

.system-title h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: white;
}

.system-subtitle {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
  margin-top: 4px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  margin-right: 16px;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  font-size: 14px;
}

.logout-btn {
  color: white;
  font-size: 14px;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* 底部区域样式 */
.bottom-container {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.menu-aside {
  width: 220px;
  background-color: #304156;
  overflow-y: auto;
}

.menu-container {
  border: none;
  height: 100%;
}

.content-main {
  flex: 1;
  padding: 0;
  background-color: #f5f5f5;
  overflow: hidden;
}

.content-area {
  padding: 20px;
  background: white;
  margin: 0 16px 16px 16px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  min-height: calc(100vh - 240px);
  overflow-y: auto;
}

/* 菜单样式优化 */
:deep(.el-menu) {
  border-right: none;
  background-color: #304156 !important;
}

:deep(.el-menu--vertical) {
  background-color: #304156 !important;
}

:deep(.el-sub-menu__title) {
  height: 50px;
  line-height: 50px;
  color: #bfcbd9 !important;
  background-color: #304156 !important;
}

:deep(.el-sub-menu__title:hover) {
  background-color: #263445 !important;
  color: #409eff !important;
}

:deep(.el-menu-item) {
  height: 50px;
  line-height: 50px;
  color: #bfcbd9 !important;
  background-color: #304156 !important;
}

:deep(.el-menu-item:hover) {
  background-color: #263445 !important;
  color: #409eff !important;
}

:deep(.el-menu-item.is-active) {
  background-color: #409eff !important;
  color: #ffffff !important;
}

:deep(.el-sub-menu .el-menu-item) {
  background-color: #1f2d3d !important;
  color: #bfcbd9 !important;
  padding-left: 50px !important;
}

:deep(.el-sub-menu .el-menu-item:hover) {
  background-color: #263445 !important;
  color: #409eff !important;
}

:deep(.el-sub-menu .el-menu-item.is-active) {
  background-color: #409eff !important;
  color: #ffffff !important;
}

:deep(.el-sub-menu.is-active .el-sub-menu__title) {
  color: #409eff !important;
}

:deep(.el-sub-menu.is-opened .el-sub-menu__title) {
  color: #409eff !important;
}

/* 菜单图标样式 */
:deep(.el-menu-item .el-icon),
:deep(.el-sub-menu__title .el-icon) {
  color: inherit;
  margin-right: 8px;
}

/* 子菜单箭头样式 */
:deep(.el-sub-menu__icon-arrow) {
  color: #bfcbd9;
}

:deep(.el-sub-menu.is-active .el-sub-menu__icon-arrow) {
  color: #409eff;
}

:deep(.el-sub-menu.is-opened .el-sub-menu__icon-arrow) {
  color: #409eff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 16px;
  }
  
  .system-title h1 {
    font-size: 20px;
  }
  
  .menu-aside {
    width: 200px;
  }
  
  .content-area {
    margin: 8px;
    padding: 16px;
  }
}
</style>
